package com.example.administrator.xiaoxiang.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.bean.HomeGoodGroupItem;

import java.util.List;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;


/**
 * Created by Administrator on 2017/1/6.
 */

public class HomeGoodsAdapter extends RecyclerView.Adapter<HomeGoodsAdapter.MyViewHolder> {
    public interface OnItemClickListener{
        void OnItemClick(String url);
    }
    private HomeGoodsAdapter.OnItemClickListener onItemClickListener;
    private Context context;
    private List<HomeGoodGroupItem> data;
    private boolean isSecond;//判断是不是二级页面过来的数据
    private View view;
    public HomeGoodsAdapter(Context context, List<HomeGoodGroupItem> data) {
        this.context = context;
        this.data = data;
    }
    public HomeGoodsAdapter(Context context, List<HomeGoodGroupItem> data,boolean isSecond) {
        this.context = context;
        this.data = data;
        this.isSecond = isSecond;
    }

    public void setOnItemClickListener(HomeGoodsAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (isSecond) {
            view = LayoutInflater.from(context).inflate(R.layout.item_second_good_recyclerview,parent,false);
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.item_goods_recyclerview,parent,false);
        }
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final HomeGoodGroupItem homeGoodGroupItem = data.get(position);
        holder.subTitle.setText(homeGoodGroupItem.subTitle);
        holder.directPaymentAmount.setText("首付立减"+homeGoodGroupItem.directPaymentAmount/100);
        holder.price.setText("￥"+homeGoodGroupItem.peroidInstalmentAmount/100.0);
        holder.instalment.setText("x"+homeGoodGroupItem.instalmentPeriods+"期");
        Glide.with(context).load(homeGoodGroupItem.thumbnailUrl).placeholder(R.drawable.home_goods_small_nothing).into(holder.imageView);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.OnItemClick(homeGoodGroupItem.goodsCode);
                    Log.e(TAG, "onClick: "+homeGoodGroupItem.goodsCode);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView subTitle;
        TextView price;
        TextView instalment;
        TextView directPaymentAmount;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView_goods_recyclerView);
            subTitle = (TextView) itemView.findViewById(R.id.textView_goods_recyclerView);
            price = (TextView) itemView.findViewById(R.id.price);
            instalment = (TextView) itemView.findViewById(R.id.instalment);
            directPaymentAmount = (TextView) itemView.findViewById(R.id.directPay);
        }
    }
}
